$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'capital/supplierapplycashrule/list',
        datatype: "json",
        colModel: [			
			{ label: 'ID', name: 'ruleId', index: 'rule_id', width: 50, key: true, align: 'center' },
			{ label: '工厂', name: 'factoryName', index: 'factory_name', width: 150 , align: 'center'},
			{ label: '规则类型', name: 'ruleType', index: 'rule_type', width: 80 ,align: 'center',
                formatter: function (value) {
					return value == '2' ? '日结' : '月结';
                }
			},
			{ label: '月结时间', name: 'ruleMonth', index: 'rule_month', width: 80 , align: 'center',
                formatter: function (value, options, row) {
                    var ruleType = row.ruleType;
                    return ruleType == '1' ? value : '-';
                }
			},
			{ label: '日结天数', name: 'ruleDay', index: 'rule_day', width: 80 , align: 'center',
                formatter: function (value, options, row) {
                    var ruleType = row.ruleType;
                    return ruleType == '2' ? value : '-';
                }
			},
			{ label: '更新人', name: 'updateName', index: 'update_name', width: 80 , align: 'center'},
			{ label: '更新时间', name: 'updateTime', index: 'update_time', width: 120 , align: 'center'},
			{ label: '创建人', name: 'createName', index: 'create_name', width: 80 , align: 'center'},
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 120 , align: 'center'}
        ],
		viewrecords: true,
        height: window.innerHeight - 120,
        rowNum: 25,
        rowList : [25,50,75],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",
            userdata: "userData"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            var userData = $("#jqGrid").jqGrid('getGridParam', 'userData');
            vm.sellerInfoList = userData;
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		supplierApplyCashRule: {},
        sellerInfoList: [],
		q: {
            factoryName: ''
		}
	},
    methods: {
		query: function () {
			vm.reload();
		},
        reset: function () {
            vm.q.factoryName = '';
            vm.reload();
        },
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.supplierApplyCashRule = {};
            Vue.set(vm.supplierApplyCashRule, 'ruleType', 1);
            Vue.set(vm.supplierApplyCashRule, 'ruleMonth', '25');
		},
		update: function (event) {
			var ruleId = getSelectedRow();
			if(ruleId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(ruleId)
		},
		saveOrUpdate: function (event) {
			$.ajax({
				type: "POST",
			    url: baseURL + "capital/supplierapplycashrule/update",
                contentType: "application/json",
			    data: JSON.stringify(vm.supplierApplyCashRule),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ruleIds = getSelectedRows();
			if(ruleIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "capital/supplierapplycashrule/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ruleIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(ruleId){
			$.get(baseURL + "capital/supplierapplycashrule/info/"+ruleId, function(r){
                vm.supplierApplyCashRule = r.supplierApplyCashRule;
                console.info(vm.supplierApplyCashRule);
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'factoryName': vm.q.factoryName,
                },
                page:page
            }).trigger("reloadGrid");
		}
	}
});